var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), i = require("../../../utils/arrayUtil.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    inputVal: "",
    searchList: [],
    searchResult: !1,
    hasConcent: !0,
    hotWordList: [],
    historyArray: [],
    pageindex: 1,
    pagemax: 1,
    showLoading: false,
    searchContent: "",
    keyWordList: [],
    isEnd: !1,
    splitstart: 0,
    isLoading:true,
    loading: false,
    netWork: true,
    lastpg: false,
    lastList: [],
    defaultWord: "搜索您需要的商品与服务",
    isiOs: !0,
    currentTab:0,
    merList:[],
    jd:0,
    wd:0,
  },
  goBackPreRoute: function () {
    wx.navigateBack({ delta: 1 });
  },
  pushToAd: function (a) {
    d.pushToAd(a, "search");
  },
  pushToDetail_2: function (t) {
    console.log("进入详情页", t);
    var d = t.detail.target.dataset.item.id, s = t.detail.formId;
    // e.advertisingClick(s, 1, d), a.shareInfo("search", "appSearch", this.data.inputVal, d);
    var i = "/pages/product/detail/index?id=" + d + "&noSku=noSku&_from=appSearch&param1=" + this.data.inputVal;
    wx.navigateTo({ url: i });
  },
  bindClearHistory: function () {
    var that = this;
    wx.removeStorage({
      key: "historySearch",
      success: function (t) {
        console.log("清空历史搜索：", t),
          that.setData({
            historyArray: []
          });
      }
    });
  },
  saveHistorySearch: function (a) {
    var that = this, e = this.data.historyArray;
    e.map(function (t, d) {
      t == a && e.splice(d, 1);
    }), e.push(a),
      wx.setStorage({
        key: "historySearch",
        data: e,
        success: function () {
          that.setData({
            historyArray: e
          });
        }
      });
  },
  searchHisItem: function (a) {
    console.log("点击hisitem:", a.currentTarget.dataset.hisitem), this.setData({
      inputVal: a.currentTarget.dataset.hisitem,
      pageindex: 1,
      searchList: [],
      splitstart: 0,
      lastList: [],
      isLoading: !0
    }), this.fetchSearchData(a.currentTarget.dataset.hisitem, 1);
  },
  bindSearch: function () {
    this.setData({
      pageindex: 1,
      searchList: [],
      splitstart: 0,
      lastList: []
    })
    if(this.data.inputVal.length > 0){
      console.log("调用搜索"), 
      this.setData({ isLoading: !0 })
      if(this.data.currentTab == 1){
        this.fetchSearchData(this.data.inputVal, 1)
      }else{
        // this.modalcnt(this.data.inputVal);
        this.onHttpMerList(keyword);
      }
    }else{
      wx.showModal({
        title: "提示",
        content: "请输入搜索内容",
        showCancel: !1
      });
    }
  },
  // modalcnt:function  (keyword)  {
  //   var that = this
  //   that.data.pageIndex = 1
  //   //获取经纬度  
  //   wx.getLocation({
  //     type:  'gcj02',
  //     success:  function  (res)  {
  //     console.log('modalcnt', res)
  //     var latitude = res.latitude
  //     var longitude = res.longitude
  //     that.data.wd = latitude//纬度
  //     that.data.jd = longitude//经度
  //     app.setStorageSync("lng",longitude);
  //     app.setStorageSync("lat",latitude);
  //     that.onHttpMerList(keyword);
  //   },fail:function(res){
  //       that.data.wd = 0
  //       that.data.jd = 0
  //       that.onHttpMerList(keyword)
  //     }
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var d = this;
    wx.getStorage({
      key: "historySearch",
      success: function (a) {
        console.log("historySearch:", a), 
        d.setData({ 
          historyArray: a.data  
        });
      }
    })
    this.onHttpMerList();
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost("/index/hotkeyword", {}, function (a) {
      console.log("keyWord:", a)
      that.setData({
        hotWordList: a.data.data.words,
        adBanner: a.data.data.banner,
        hasBanner: a.data.data.hasBanner,
        isLoading: false
      });
    }, function (a) {
      console.log("keyWork err:", a);
    });
  },
  serchOtherWord: function (a) {
    var t = a.currentTarget.dataset.word;
    this.setData({
      inputVal: t,
      pageindex: 1,
      searchList: [],
      splitstart: 0,
      lastList: [],
      isLoading: !0
    })
    this.fetchSearchData(t, 1);
  },
  handleChange({ detail }) {
    this.data.currentTab = detail.index;
    this.setData({
      isLoading:true,
      currentTab: detail.index
    })

    if(this.data.currentTab == 0){
      this.onHttpMerList(this.data.inputVal || this.data.searchContent)
    }else{
      this.fetchSearchData(this.data.inputVal || this.data.searchContent, 1)
    }
  },

  /**
   * 获取页面数据的 http请求
   */
  onHttpMerList: function (keyword) {
    var that = this
    fetchApi.httpPost('/mer/search/list',{
      keyword:keyword,
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
      longitude: that.data.jd,
      latitude: that.data.wd,
    }, function (res) {
      console.log("获取搜索的商家列表", res)
      if(res.data.totalcount > 0){
        that.data.pageIndex = that.data.pageIndex + 1;
        var mers = res.data.data;
        if(mers.length > 0){
          for (var index in mers) {
            that.data.merList.splice(that.data.merList.length, 0, mers[index]);
          }
          that.setData({ 
            hasConcent:true,
            searchResult:true,
            merList: that.data.merList,
          });
        }
      }
      that.setData({ isLoading: false, loading: true, netWork: true })
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
  fetchSearchData: function (keyword, pageIndex) {
    var that = this;
    if (this.data.searchList && this.data.searchList.length < 10 && pageIndex > 1) 
    return this.setData({
      showLoading: false,
      isEnd: true
    });
    var r = {
      keyword: keyword,
      pageIndex: pageIndex,
      pageSize:10
    }
    that.saveHistorySearch(keyword)
    fetchApi.httpPost("/product/list", r, function (a) {
      console.log("search data:", a)
      if (a.data.code == 0)
        that.data.pageindex = that.data.pageindex + 1;
        if (a.data.data.length > 0 && !a.data.data.recommend) {
          var r = [], h = a.data.data.otherWord && a.data.data.otherWord.length > 0 ? a.data.data.otherWord : "";
          r = a.data.data.aditem && a.data.data.aditem.length > 0 ? a.data.data.aditem[0].item 
          ? a.data.data : a.data.data.aditem.concat(a.data.data) : a.data.data;
          that.setData({
            searchList: 1 == pageIndex ? r : that.data.searchList.concat(r),
            searchResult: true,
            hasConcent: true,
            isLoading:false,
            showLoading: false,
            searchContent: keyword,
            recommendWord: a.data.data.recommendWord ? a.data.data.recommendWord : "",
            otherWord: h,
          })
        }else{
          that.setData({
            searchResult: true,
            hasConcent: false,
            isLoading: false,
            showLoading: false,
          })
        }
    }, function (a) {
      that.setData({ isLoading: false ,
        showLoading: false,}),
      console.log("search data err:", a);
    });
  },
  clearInput: function () {
    this.setData({ inputVal: "" });
  },
  inputTyping: function (a) {
    console.log("输入框：", a.detail.value), 
    this.setData({ inputVal: a.detail.value })
    if(this.data.currentTab == 0){
      this.data.pageIndex = 1
      this.setData({
        merList:[],
        isLoading :true
      })
      this.onHttpMerList(a.detail.value);
    }else{
      this.fetchSearchData(a.detail.value,1)
    }
  },
  cancleInput: function () {
    this.data.searchList.length > 0 ? this.setData({
      searchResult: !0
    }) : wx.navigateBack({
      delta: 1
    });
  },
  touchSearch: function () {
    this.setData({ searchResult: !1 });
  },
  onReachBottom: function () {
    console.log("上啦加载")
    if(this.data.lastpg){
      this.setData({
        showLoading: !1,
        isEnd: !0
      })
    }else{
      this.setData({
        showLoading: !0,
        isEnd: !1
      })
    };
    if(!this.data.inputVal && !this.data.searchContent){
      if(this.data.currentTab == 0){
        this.onHttpMerList(this.data.inputVal || this.data.searchContent)
      }else{
        this.fetchSearchData(this.data.inputVal || this.data.searchContent, this.data.pageindex + 1)
      }
    }
  }
})